home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’93 / TwilightZone / source / events.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-18  |  7.1 KB  |  390 lines  |  [TEXT/KAHL]

  1. /*-------------------------------------------------------------------------------------
  2.  *
  3.  * Simple Sample Application Framework
  4.  *
  5.  * ©1991 Apple Computer
  6.  *
  7.  -------------------------------------------------------------------------------------*/
  8. /*
  9.  * events.c -- main event loop and basic event handling
  10.  *
  11.  * change history:
  12.  *
  13.  * SJF        11/6/91        1.0d1        initial coding
  14.  *
  15.  */
  16.  
  17. #include <AppleEvents.h>
  18.  
  19. #include "const.h"
  20. #include "mytypes.h"
  21. #include "mymenus.h"
  22. #include "globals.h"
  23. #include "utils.h"
  24. #include "windowstuff.h"
  25. #include "commands.h"
  26. #include "aevt.h"
  27.  
  28. #include "events.h"
  29.  
  30. /* main event loop */
  31.  
  32. void MainLoop(void)
  33. {
  34.     EventRecord ev;
  35.     Boolean gotEvent;
  36.     
  37.     while (!gDone) {
  38.         if (gHasWaitNextEvent)
  39.             gotEvent = WaitNextEvent(everyEvent,&ev,0/*SleepTime()*/,nil);
  40.         else {
  41.             gotEvent = GetNextEvent(everyEvent,&ev);
  42.             SystemTask();
  43.         }
  44.         
  45.         ProcessEvent(&ev);
  46.     }
  47. }
  48.  
  49.  
  50. /* calculate how long to "wait" */
  51.  
  52. long SleepTime(void)
  53. {
  54.     if (gInBackground)
  55.         return kSleepBackground;
  56.     else
  57.         return kSleepForeground;
  58. }
  59.  
  60.  
  61. /* set cursor shape depending on mouse location */
  62.  
  63. void HandleFixCursor(Point where,RgnHandle theRgn)
  64. {
  65. }
  66.  
  67.  
  68. /* idle time processing */
  69.  
  70. void HandleIdle(WindowPtr window)
  71. {
  72.     WindowPtr iWindow;
  73.     
  74.     for (iWindow=FrontWindow(); IsAppWindow(iWindow); iWindow=(WindowPtr)((WindowPeek)iWindow)->nextWindow)
  75.         SendWindowMessage(iWindow,kIdleMessage,nil);
  76. }
  77.  
  78.  
  79. /* process an event gotten by MainLoop() */
  80.  
  81. void ProcessEvent(EventRecord *ev)
  82. {
  83.     void *returnResult;
  84.     
  85.     returnResult = SendWindowMessage(FrontWindow(),kEventMessage,ev);
  86.     if (returnResult!=nil)
  87.         return;
  88.  
  89.     switch (ev->what) {
  90.             case mouseDown:
  91.                 HandleMouseDowns(ev);
  92.                 break;
  93.             case keyDown:
  94.             case autoKey:
  95.                 HandleKeyDowns(ev);
  96.                 break;
  97.             case updateEvt:
  98.                 HandleUpdates((WindowPtr)ev->message);
  99.                 break;
  100.             case activateEvt:
  101.                 HandleActivates(ev);
  102.                 break;
  103.             case osEvt:
  104.                 HandleSREvt(ev->where,ev->message);
  105.                 break;
  106.             case kHighLevelEvent:
  107.                 DoHighLevelEvent(ev);
  108.                 break;
  109.             case nullEvent:
  110.                 HandleIdle(FrontWindow());
  111.                 break;
  112.     }
  113. }
  114.  
  115.  
  116. /* Handles suspend and resume events */
  117.  
  118. void HandleSREvt(Point where,long message)
  119. {
  120.     extern NMRec *gNotify;
  121.     extern Boolean gInBackground;
  122.     unsigned long whatMessage;
  123.     
  124.     whatMessage = message >> 24;
  125.     
  126.     if (whatMessage==suspendResumeMessage) {
  127.         if ((message & 1) != 0) {
  128.             gInBackground = false;
  129.             SetCursor(&qd.arrow);
  130.             if (FrontWindow()) {
  131.                 HiliteWindow(FrontWindow(),true);
  132.                 DoActivate(FrontWindow(),true);
  133.             }
  134.         }
  135.         else if (FrontWindow()) {
  136.             gInBackground = true;
  137.             HiliteWindow(FrontWindow(),false);
  138.             DoDeActivate(FrontWindow(),true);
  139.         }
  140.     }
  141.     else if ((whatMessage&mouseMovedMessage)==mouseMovedMessage)
  142.         HandleFixCursor(where,gCursorRgn);
  143. }
  144.  
  145.  
  146. /* Handles activate and deactivate events for a window */
  147.  
  148. void HandleActivates(EventRecord *ev)
  149. {
  150.     if ((ev->modifiers & activeFlag) != 0) {
  151.         DoActivate((WindowPtr)ev->message,((ev->modifiers & 0x0002) != 0));
  152.     }
  153.     else {
  154.         DoDeActivate((WindowPtr)ev->message,((ev->modifiers & 0x0002) != 0));
  155.     }
  156. }
  157.  
  158.  
  159. /* Handles activate events for a window */
  160.  
  161. void DoActivate(WindowPtr window,Boolean chFlag)
  162. {
  163.     SendWindowMessage(window,kActivateMessage,&chFlag);
  164. }
  165.  
  166.  
  167. /* Handles deactivate events for a window */
  168.  
  169. void DoDeActivate(WindowPtr window,Boolean chFlag)
  170. {
  171.     SendWindowMessage(window,kDeactivateMessage,&chFlag);
  172. }
  173.  
  174.  
  175. /* handles update events for a window */
  176.  
  177. void HandleUpdates(WindowPtr window)
  178. {
  179.     GrafPtr savePort;
  180.     
  181.     GetPort(&savePort);
  182.     SetPort(window);
  183.     
  184.     BeginUpdate(window);    // hack alert!
  185.     EndUpdate(window);
  186.  
  187.     SendWindowMessage(window,kUpdateMessage,nil);
  188.     SetPort(savePort);
  189. }
  190.  
  191.  
  192. /* handles program keydowns */
  193.  
  194. void HandleKeyDowns(EventRecord *ev)
  195. {
  196.     short theChar;
  197.     WindowPtr window;
  198.     
  199.     theChar = ev->message & charCodeMask;
  200.     if ((ev->modifiers & cmdKey) != 0)
  201.         DoMenuCommand(MenuKey(theChar));
  202.     else if (IsAppWindow(window=FrontWindow()))
  203.         SendWindowMessage(window,kKeyMessage,&theChar);
  204. }
  205.  
  206.  
  207. /* handles mouse down events for a window */
  208.  
  209. void HandleMouseDowns(EventRecord *ev)
  210. {
  211.     WindowPtr window;
  212.     short part;
  213.     
  214.     part = FindWindow(ev->where,&window);
  215.     
  216.     switch (FindWindow(ev->where,&window)) {
  217.         case inMenuBar:
  218.             DoMenuCommand(MenuSelect(ev->where));
  219.             break;
  220.         case inSysWindow:
  221.             SystemClick(ev,window);
  222.             break;
  223.         case inDrag:
  224.             DoDrag(window,ev->where);
  225.             break;
  226.         case inGrow:
  227.             DoGrow(window,ev->where);
  228.             break;
  229.         case inGoAway:
  230.             if (TrackGoAway(window,ev->where)) {
  231.                 CommCloseWindow(window);
  232.             }
  233.             break;
  234.         case inZoomIn:
  235.         case inZoomOut:
  236.             if (TrackBox(window,ev->where,FindWindow(ev->where,&window)))
  237.                 DoZoom(window,FindWindow(ev->where,&window));
  238.             break;
  239.         case inContent:
  240.             DoContentClick(window,ev);
  241.             break;
  242.     }
  243. }
  244.  
  245.  
  246. /* handles window drag events */
  247.  
  248. void DoDrag(WindowPtr window,Point globMouse)
  249. {
  250.     Rect dragRect = kWindowDragLimits;
  251.         
  252.     DragWindow(window,globMouse,&dragRect);
  253.     SendWindowMessage(window,kResizeMessage,&window->portRect);
  254.     SetPort(window);
  255. }
  256.  
  257.  
  258. /* handles window grow events */
  259.  
  260. void DoGrow(WindowPtr window,Point globMouse)
  261. {
  262.     long newSize;
  263.     Rect windLimits = kWindowGrowLimits;
  264.     Rect oldSize;
  265.     GrafPtr tempPort;
  266.     
  267.     oldSize = window->portRect;
  268.     if ((newSize = GrowWindow(window,globMouse,&windLimits)) != 0) {
  269.         GetPort(&tempPort);
  270.         SetPort(window);
  271.         SizeWindow(window,LoWord(newSize),HiWord(newSize),true);
  272.         SendWindowMessage(window,kResizeMessage,&oldSize);
  273.         InvalRect(&window->portRect);
  274.         SetPort(tempPort);
  275.     }
  276. }
  277.  
  278.  
  279. /* handles window zooms */
  280.  
  281. void DoZoom(WindowPtr window,short part)
  282. {
  283.     GrafPtr tempPort;
  284.     
  285.     GetPort(&tempPort);
  286.     SetPort(window);
  287.     ZoomWindow(window,part,true);
  288.     SendWindowMessage(window,kResizeMessage,nil);
  289.     InvalRect(&window->portRect);
  290.     SetPort(tempPort);
  291. }
  292.  
  293.  
  294. /* handles a click in a window content region */
  295.  
  296. void DoContentClick(WindowPtr window,EventRecord *ev)
  297. {
  298.     GrafPtr savePort;
  299.     
  300.     if (window != FrontWindow()) {
  301.         SelectWindow(window);
  302.         return;
  303.     }
  304.     
  305.     SendWindowMessage(window,kClickMessage,ev);        
  306. }
  307.  
  308.  
  309. /* handles menu commands */
  310.  
  311. void DoMenuCommand(long mResult)
  312. {
  313.     short selItem,selMenu,temp;
  314.     Str255 name;
  315.     Boolean flag;
  316.     GrafPtr tempPort;
  317.     MenuHandle theMenu;
  318.     
  319.     selItem = LoWord(mResult);
  320.     selMenu = HiWord(mResult);
  321.     switch (selMenu) {
  322.         case kAppleMenu:
  323.             if (selItem>2) {
  324.                 GetPort(&tempPort);
  325.                 SetCursor(&qd.arrow);
  326.                 theMenu = GetMHandle(kAppleMenu);
  327.                 GetItem(theMenu,selItem,name);
  328.                 temp = OpenDeskAcc(name);
  329.                 SetPort(tempPort);
  330.             }
  331.             else CommAbout();
  332.             break;
  333.         case kFileMenu:
  334.             switch (selItem) {
  335.                 case kNewItem:
  336.                     CommNew();
  337.                     break;
  338.                 case kOpenItem:
  339.                     CommOpen();
  340.                     break;
  341.                 case kCloseItem:
  342.                     CommCloseWindow(FrontWindow());
  343.                     break;
  344.                 case kSaveItem:
  345.                     CommSaveFile(FrontWindow());
  346.                     break;
  347.                 case kSaveAsItem:
  348.                     CommSaveAsFile(FrontWindow());
  349.                     break;
  350.                 case kPageSetupItem:
  351.                     CommPageSetup(FrontWindow());
  352.                     break;
  353.                 case kPrintItem:
  354.                     CommPrint(FrontWindow());
  355.                     break;
  356.                 case kQuitItem:
  357.                     gDone = true;
  358.                     break;
  359.             }
  360.             break;
  361.         case kEditMenu:
  362.             if (!(SystemEdit(selItem-1)))
  363.                 CommEdit(FrontWindow(),selItem);
  364.             break;
  365.      }
  366.     HiliteMenu(0);
  367. }
  368.  
  369.  
  370. OSErr HandleOpenDoc(FSSpec *fSpec)
  371. {
  372.     LoOpen(fSpec);
  373.     return noErr;
  374. }
  375.  
  376.  
  377. OSErr HandlePrintDoc(FSSpec *fSpec)
  378. {
  379. }
  380.  
  381.  
  382. void ExitProgram(void)
  383. {
  384.     WindowPtr window;
  385.     
  386.     while (window=FrontWindow())
  387.         CommCloseWindow(window);
  388. }
  389.  
  390.